#include "Rp2AlphaState.h"
#include "Rp2Renderer.h"
// 
// #define GT_EXTERN extern
// extern "C"
// {
// 
// #define glBlendColor GTglBlendColor
// 
// GT_EXTERN void GTglBlendColor (GLclampf fRed, GLclampf fGreen,
//     GLclampf fBlue, GLclampf fAlpha);
// 
// #define GT_ENTER_FUNCTION(glFunction)
// #define GT_NONNULL_FUNCTION(glFunction)
// // This function asserts when a null function is encountered.  The renderer
// // is expecting the function to exist.
// // extern void ReportNullFunction(const char*);
// // #define GT_NULL_FUNCTION(glFunction) ReportNullFunction(#glFunction)
// // This function checks glGetError after each OpenGL function call.
// // extern void ReportGLError(const char*);
// // #define GT_EXIT_FUNCTION(glFunction) ReportGLError(#glFunction)
// 
// // Enable this block to disable error checking.
// #define GT_NULL_FUNCTION(glFunction)
// #define GT_EXIT_FUNCTION(glFunction)
// 
// #define GT_BODY_A4(f,a0,a1,a2,a3)\
// GT_ENTER_FUNCTION(f); \
// if (f) \
// { \
//     GT_NONNULL_FUNCTION(f); \
//     f(a0,a1,a2,a3); \
// } \
// else \
// { \
//     GT_NULL_FUNCTION(f); \
// } \
// GT_EXIT_FUNCTION(f)
// 
// #define STDCALL __stdcall
// #define EXTERN __declspec(dllimport)
// typedef void (STDCALL* PsglBlendColor) (GLclampf, GLclampf, GLclampf,
//     GLclampf);
// static PsglBlendColor sglBlendColor = 0;
// 
// void GTglBlendColor (GLclampf fRed, GLclampf fGreen, GLclampf fBlue,
//     GLclampf fAlpha)
// {
//     GT_BODY_A4(sglBlendColor,fRed,fGreen,fBlue,fAlpha);
// }
// 
// }

using namespace Rp2;

// map to opengl states

GLenum Renderer::ms_aeAlphaSrcBlend[AlphaState::SBF_QUANTITY] = 
{
    GL_ZERO,
    GL_ONE,
    GL_DST_COLOR,
    GL_ONE_MINUS_DST_COLOR,
    GL_SRC_ALPHA,
    GL_ONE_MINUS_SRC_ALPHA,
    GL_DST_ALPHA,
    GL_ONE_MINUS_DST_ALPHA,
    GL_SRC_ALPHA_SATURATE,
    GL_CONSTANT_COLOR,
    GL_ONE_MINUS_CONSTANT_COLOR,
    GL_CONSTANT_ALPHA,
    GL_ONE_MINUS_CONSTANT_ALPHA
};

GLenum Renderer::ms_aeAlphaDstBlend[AlphaState::DBF_QUANTITY] = 
{
    GL_ZERO,
    GL_ONE,
    GL_SRC_COLOR,
    GL_ONE_MINUS_SRC_COLOR,
    GL_SRC_ALPHA,
    GL_ONE_MINUS_SRC_ALPHA,
    GL_DST_ALPHA,
    GL_ONE_MINUS_DST_ALPHA,
    GL_CONSTANT_COLOR,
    GL_ONE_MINUS_CONSTANT_COLOR,
    GL_CONSTANT_ALPHA,
    GL_ONE_MINUS_CONSTANT_ALPHA
};

GLenum Renderer::ms_aeAlphaTest[AlphaState::TF_QUANTITY] =
{
    GL_NEVER,
    GL_LESS,
    GL_EQUAL,
    GL_LEQUAL,
    GL_GREATER,
    GL_NOTEQUAL,
    GL_GEQUAL,
    GL_ALWAYS
};
//---------------------------------------------------------------------------------------------------
void Renderer::SetAlphaState(AlphaState* pkAlphaState)
{
	// set global state
    m_aspkState[GlobalState::ALPHA] = pkAlphaState;

	if (pkAlphaState->BlendEnabled)
	{
		glEnable(GL_BLEND);
		glBlendFunc(ms_aeAlphaSrcBlend[pkAlphaState->SrcBlend],
			ms_aeAlphaDstBlend[pkAlphaState->DstBlend]);
	}
	else
	{
		glDisable(GL_BLEND);
	}

	if (pkAlphaState->TestEnabled)
	{
		glEnable(GL_ALPHA_TEST);
		glAlphaFunc(ms_aeAlphaTest[pkAlphaState->Test], pkAlphaState->Reference);
	}

//	** An error may occur with this function	
	glBlendColor(pkAlphaState->ConstantColor.R(), pkAlphaState->ConstantColor.G(),
		pkAlphaState->ConstantColor.B(), pkAlphaState->ConstantColor.A());
/*	glBlendColor(0.5f, 0.5f, 0.5f, 0.50f);*/
}
//---------------------------------------------------------------------------------------------------